package com.wt.dao;


import com.wt.dao.idao.IOrderDao;
import com.wt.entity.Order;
import com.wt.mapper.impl.OrderRowMapper;
import com.wt.util.DBHelper;

import java.sql.SQLException;
import java.util.List;

public class OrderDao implements IOrderDao {
    DBHelper<Order> dbHelper = new DBHelper<>();

    /**
     * 查询所以顶大
     * @param name 用户名称
     * @return 订单集合
     * @throws SQLException
     */
    @Override
    public List<Order> selectAllOrder(String name) throws SQLException {
        dbHelper.getConnection();
        String sql = "select * from goorder where user_id = ?";
        List<Order> orders = dbHelper.executeQuery(sql,new OrderRowMapper(),name);
        dbHelper.closeConnection();
        return orders;
    }

    /**
     * 根据订单编号修改订单状态
     * @param name 返回用户订单集合
     * @param orderId 订单id
     * @param stater 订单状态
     * @return 订单集合
     * @throws SQLException
     */
    @Override
    public Boolean updateOrderStater(Integer orderId, Integer stater) throws SQLException {
        dbHelper.getConnection();
        String sql = "update goorder set order_stater=? where order_id=?";
        int rows = dbHelper.executeUpdate(sql,stater,orderId);
        dbHelper.closeConnection();
        return rows == 1;

    }

    /**
     * 添加订单
     * @param order 订单信息
     * @return 该用户全部订单
     * @throws SQLException
     */
    @Override
    public Boolean addOrder(Order order) throws SQLException {
        dbHelper.getConnection();
//        String sql = "insert into goorder values ('三日游',0,'2021-10-27 14:10:43','2021-10-16 16:20:20',699.0,'/jialingjiang.jpg','诗殇丶月',null)";
        String sql = "insert into goorder values (?,0,?,?,?,?,?,null)";
        int rows = dbHelper.executeUpdate(sql,order.getOrderName(),order.getOrderTime(),order.getTravelTime(),
                order.getOrderPrice(),order.getScenicImage(),order.getUserId());
        dbHelper.closeConnection();
        return rows == 1;
    }

    /**
     * 取消、删除订单
     * @param orderId 订单号
     * @return boolean
     * @throws SQLException
     */
    @Override
    public Boolean delOrder(Integer orderId) throws SQLException {
        dbHelper.getConnection();
        String sql = "delete from goorder where order_id=?";
        int rows = dbHelper.executeUpdate(sql,orderId);
        return rows == 1;
    }


}
